import router from 'umi/router';
import { message } from 'antd';
import {
  getAllDevices,
  getAllDomainGroup,
  getCache,
  addCache,
  updateCache,
  getTranspondList,
  addTranspond,
  updateTranspond,
  deleteTranspond,
  freshConfig

} from './services';

export default {
  namespace: 'transpond',
  state: {
    allDevices: [],
    allDomainGroup:[],
    loading: false,
    confLoading: false,
    list: [],
    pageSize: 10,
    pageNum: 1,
    cache: {}
  },

  effects: {
    /**
     * 缓存
     */
    *getCache({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeConfLoading',
          payload: true,
        });
        const res = yield call(getCache, payload);
        yield put({
          type: 'save',
          payload: {
            cache: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeConfLoading',
          payload: false,
        });
      }
    },

    *addCache({ payload, callback }, { call, put }) {
      try {
        yield call(addCache, payload);
        message.info('配置成功');
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

    *updateCache({ payload, callback }, { call, put }) {
      try {
        yield call(updateCache, payload);
        message.info('修改成功');
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

    *getAllDevices({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllDevices, payload);
        if(res.data.length>0) {
          callback(res.data[0]);
        }
        yield put({
          type: 'save',
          payload: {
            allDevices: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },
    *getAllDomainGroup({ payload }, { call, put }) {
      try {
        const res = yield call(getAllDomainGroup, payload);
        yield put({
          type: 'save',
          payload: {
            allDomainGroup: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },
    

    /**
     * 转发
     */

    *getTranspondList({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getTranspondList, payload);
        yield put({
          type: 'save',
          payload: {
            list: res.pageData,
            total: res.total,
            pageNum: res.pageNum,
            pageSize: res.pageSize,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *addTranspond({ payload, callback }, { call, put, select }) {
      try {
        yield call(addTranspond, payload);
        const total = yield select(state => state.transpond.total);
        const pageSize = yield select(state => state.transpond.pageSize);
        const pageNum = Math.ceil((total + 1) / pageSize);
        callback(pageNum, pageSize);
      } catch (e) {
        message.error(e.message);
      }
    },

    *updateTranspond({ payload, callback }, { call, put, select }) {
      try {
        yield call(updateTranspond, payload);
        const pageNum = yield select(state => state.transpond.pageNum);
        const pageSize = yield select(state => state.transpond.pageSize);
        message.info('修改成功');
        callback(pageNum, pageSize);
      } catch (e) {
        message.error(e.message);
      }
    },

    *freshConfig({ payload, callback }, { call, put, select }) {
      try {
        yield call(freshConfig, payload);
        message.info("启用成功")
      } catch (e) {
        message.error(e.message);
      }
    },

    *deleteTranspond({ payload, callback }, { call, put, select }) {
      try {
        yield call(deleteTranspond, payload);
        const pageSize = yield select(state => state.transpond.pageSize);
        const preTotal = yield select(state => state.transpond.total);
        const curPage = yield select(state => state.transpond.pageNum);
        let curTotal = preTotal - payload.ids.length;
        const lastPage = Math.ceil(curTotal / 10);
        let pageNum = Math.min.apply(null, [curPage, lastPage]);
        message.info('操作成功');
        callback(pageNum, pageSize);
      } catch (e) {
        message.error(e.message);
      }
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },

    changeConfLoading(state, { payload }) {
      return {
        ...state,
        confLoading: payload,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
